package com.boang.service.impl;

import com.boang.entity.dto.SmsChannelDO;
import com.boang.service.SmsService;
import com.boang.util.HttpUtils;
import jakarta.annotation.Resource;
import org.apache.http.HttpResponse;
import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/*
  @author: 13813
  @date: 2025/3/4 14:11
*/
@Service
public class SmsServiceImpl implements SmsService {

    @Resource
    private SmsChannelDO smsChannelDO;

    @Override
    public String sendSms(String phoneNumber, String message) throws Exception {
        String host = smsChannelDO.getHost();
        String path = smsChannelDO.getPath();
        String method = "POST";
        String appcode = smsChannelDO.getAppcode();
        Map<String, String> headers = new HashMap<String, String>();
        headers.put("Authorization", "APPCODE " + appcode);
        headers.put("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        Map<String, String> querys = new HashMap<String, String>();
        Map<String, String> bodys = new HashMap<String, String>();
        bodys.put("content", "code:" + message);
        bodys.put("templateid", "lxym_20111_sdgsfhwqgvyh");
        bodys.put("mobile", phoneNumber);

        HttpResponse response = HttpUtils.doPost(host, path, method, headers, querys, bodys);
        return EntityUtils.toString(response.getEntity());
    }
}
